package io.reader;

import framework.AbstractTest;
import utils.Log;
import utils.StringUtils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * Created by shane on 2018/5/20.
 */
public class BufferedInputFile extends AbstractTest {
    protected static final String TAG = BufferedInputFile.class.getName();

    @Override
    public void test(String[] args) {
        String testFilePath = getDefInputFile(args);

        try {
            Log.v(read(testFilePath));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected String getTag() {
        return TAG;
    }

    public static String read(String filename) throws IOException {
        if (StringUtils.isEmpty(filename)) {
            return null;
        }

        BufferedReader in = new BufferedReader(new FileReader(filename));
        String s;
        StringBuilder sb = new StringBuilder();
        while ((s = in.readLine()) != null) {
            sb.append(s);
            sb.append("\n");
        }
        in.close();
        return sb.toString();
    }
}
